{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<style>
    .layui-tree-entry{font-size:15px; line-height:24px}
    .layui-tree-set{padding:2px 0}
    .layui-tree-iconClick .layui-icon{color:#1E9FFF}
    .layui-icon layui-icon-file{font-size:16px;}
    .layui-tree-icon {height: 15px;width: 15px; text-align: center;border: 1px solid #1E9FFF; color:#1E9FFF}
    .layui-tree-line .layui-tree-set .layui-tree-set:after{top:18px;}
    .tree-left{width:200px; float:left; height:calc(100% - 30px); overflow: scroll; border:1px solid #eeeeee; background-color:#FAFAFA; padding:12px 12px 12px 5px;}
    .tree-left h3{font-size:16px; height:30px; padding-left:10px; font-weight:800}

    .gougu-upload-files{background-color: #ffffff; border:1px solid #e4e7ed;color: #c0c4cc;cursor: not-allowed; padding:0 12px; width:180px; box-sizing: border-box; display: inline-block; font-size: inherit; height: 38px; line-height: 35px; margin-right:8px; border-radius:2px;}
    .gougu-upload-tips{color:#969696}
    .layui-form-item{margin-bottom:8px;}
    .layui-input-block{min-height:24px;}
</style>
<div class="layui-col-lg12 layui-col-md12 layui-col-sm12">
    <div class="layui-col-lg3 layui-col-md3 layui-col-sm3 layui-col-xl3 layui-col-sm3 layui-col-xs3 layui-padding-3">
        <h3>物料分类</h3>
        <div id="ID-tree-demo-showLine"></div>
    </div>
    <div class="layui-col-lg9 layui-col-md9 layui-col-xl9 layui-col-sm9 layui-col-sm9 layui-col-xs9">
        <form class="layui-form gg-form-bar border-t border-x">
            <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="md_code" placeholder="物料编码" class="layui-input" autocomplete="off" />
            </div>
             <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="md_name" placeholder="物料名称" class="layui-input" autocomplete="off" />
            </div>
             <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="md_specs" placeholder="规格型号" class="layui-input" autocomplete="off" />
            </div>
            <div class="layui-input-inline" style="width:300px;">
                <select name="status" >
                    <option value="">所有状态</option>
                    {foreach ['','启用','禁用']  as $key=>  $v}
                    <option value="{$key}">{$v}</option>
                    {/foreach}
                </select>
            </div>

            <div class="layui-input-inline" style="width:150px;">
                <input type="text" style="display: none" name="type" id="seType" value="" class="layui-input" autocomplete="off" >
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webformIndex" id="webf"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
                <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
            </div>
        </form>
        <div class="p-3">
            <div>
                <table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm add-menu" lay-event="add">+ 添加Bom</button>
    <button class="layui-btn layui-btn-sm layui-btn-primary"  lay-event="import"  >导入</button>
    <a href="{__EXPORTMODE__}/Bom导入模板.xlsx" download="" class="layui-btn layui-btn-sm layui-btn-primary">导入模板</a>
    <button class="layui-btn layui-btn-sm layui-btn-danger " lay-event="dels" type="button">删除</button>
    <a class="layui-btn layui-btn-sm layui-btn-normal " target="_blank" href="{:url('exportData')}">导出</a>
</script>
<a class="layui-btn layui-btn-sm layui-btn-primary" id="import" style="display: none" >导入</a>
<script type="text/html" id="actionBar">
    {{# if(d.status == 1 ){ }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="forbid">
        <i class="fa fa-minus-square-o" aria-hidden="true"></i>禁用
    </a>
    {{# } }}

    {{# if(d.status == 2 ){ }}
    <a class="layui-btn layui-btn-xs dx_list_bottom" lay-event="resume">
        <i class="fa fa-check-square-o" aria-hidden="true"></i>启用
    </a>
    {{# } }}
    <button class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="edit">编辑</button>
    <!--    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>-->
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];
    var mStatus=['','启用','停用'];
    function gouguInit() {
        var treeTable = layui.treeTable, tool = layui.tool,form = layui.form;
        var upload=layui.upload;
        var tableIns = treeTable.render({
            elem: '#treeTable'
            , url: "{:url('index')}"
            ,toolbar: '#toolbarDemo',
            defaultToolbar:  ['filter',  'exports']
            , tree: { // treeTable 特定属性集
                customName: {name: 'name'},
                data: {},
                view: {showIcon: false},
                async: {},
                callback: {}
            }
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left' },
                {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
                {field: 'id', width: 80, title: 'ID号', align: 'center'}
                , {field: 'mdName',  title: '父件'}
                , {field: 'bar_code',  title: '物料编码'}
                , {field: 'mdSpecs',  title: '规格型号'}
                , {field: 'is_default',  title: '是否默认'}
                 , {field: 'status', title: '状态',  align: 'center',templet:function (d) {
                        return mStatus[d.status];
                    }}
                , {title: '操作',fixed: 'right',width: 200,  align: 'center'
                    , toolbar: '#actionBar'
                }
            ]]
            , page: true
        });
        //监听搜索提交
        form.on('submit(webformIndex)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //表头工具栏事件
        // $('.add-menu').on('click', function () {
        //     tool.side("/home/BomForm/add");
        //     return;
        // });

        //操作按钮
        treeTable.on('tool(treeTable)', function (obj) {
            console.log(obj.event);

            if (obj.event === 'edit') {
                tool.side("{:url('add')}?id=" + obj.data.id);
                return;
            }
            if (obj.event === 'del') {
                layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            obj.del();
                        }
                    }
                    tool.delete("{:url('delete')}", {id: obj.data.id}, callback);
                    layer.close(index);
                });
            }
            if(obj.event === 'forbid'){
                layer.confirm('确定要禁用吗?', {icon: 3, title:'提示'}, function(index){
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            tableIns.reload();
                        }
                    }
                    tool.post("{:url('doCheck')}", { id: obj.data.id,status: 2}, callback);
                    layer.close(index);
                });
            }
            if(obj.event === 'resume'){
                layer.confirm('确定要启用吗?', {icon: 3, title:'提示'}, function(index){
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            tableIns.reload();
                        }
                    }
                    tool.post("{:url('doCheck')}", { id: obj.data.id,status: 1}, callback);
                    layer.close(index);
                });
            }
        });
        treeTable.on('toolbar(treeTable)', function(obj){
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                   tool.side("{:url('add')}");
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':
                    $('#import').click()
                    break;

            };
        })
        var tree = layui.tree;
        $.ajax({
            url: "/home/api/getMaterial_type",
            type:'post',
            success:function(res){
                //仅节点左侧图标控制收缩
                tree.render({
                    elem: '#ID-tree-demo-showLine',
                    data: res.data,
                    onlyIconControl: true,  //是否仅允许节点左侧图标控制展开收缩
                    click: function(obj){
                        //layer.msg(JSON.stringify(obj.data));
                        // tableIns.reload({
                        //     where: {type: obj.data.id}
                        //     ,page:{curr:1}
                        // });
                        type_id=obj.data.id
                        $("#seType").val(type_id)
                        $("#webf").click()
                        // $('[name="keywords"]').val('');
                        // $('[name="status"]').val('');
                        // $('[name="type"]').val('');
                        layui.form.render('select');
                    }
                });
            }
        })

        var loadingIndex='';
        upload.render({
            elem: '#import'
            ,url: "{:url('importData')}"
            ,accept: 'file'
            ,exts: 'xlsx',
            before:function (res) {
                loadingIndex=layer.load(1,{
                    shade: [0.5,'#a09b9b']
                });
            }
            ,done: function(res){

                if (res.code==1&&res.data.length){
                    layer.open({
                        type:1,
                        content:res.data.join(',')
                        ,shade: 0.8
                        ,area:['600px','400px']
                        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                        ,btnAlign: 'c'
                        ,moveType: 0 //拖拽模式，0或者1
                    })
                }else {
                    layer.msg(res.msg)
                    setTimeout(function () {
                        location.reload()
                    },2000)
                }
                layer.close(loadingIndex)

            },
            error(e){
                layer.msg('网络异常请稍后再试')
                layer.close(loadingIndex)
            }
        });
    }



</script>
{/block}
<!-- /脚本 -->